package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.ContingPlanFlow;
import com.yqg.protection.mapper.ContingPlanFlowMapper;
import com.yqg.protection.service.IContingPlanFlowService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * ContingPlanFlowController 前端控制器
 *
 * @author zxf
 * @since 2024-10-15
 */
@RestController
@RequestMapping("/conting-plan-flow")
public class ContingPlanFlowController {

    @Resource
    private IContingPlanFlowService contingPlanFlowService;

    @Autowired
    private ContingPlanFlowMapper contingPlanFlowMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改应急预案流程")
    public Result save(@RequestBody ContingPlanFlow contingPlanFlow) {
        contingPlanFlow.setIsDelete(0);
        return Result.success(contingPlanFlowService.saveOrUpdate(contingPlanFlow));
    }

    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    @SysLog("删除应急预案流程")
    public Result delete(@PathVariable Integer id) {
        ContingPlanFlow contingPlanFlow = contingPlanFlowService.getById(id);
        contingPlanFlow.setIsDelete(1);
        return Result.success(contingPlanFlowService.saveOrUpdate(contingPlanFlow));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ContingPlanFlow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<ContingPlanFlow> list = contingPlanFlowService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/listbyName")
    @ApiOperation("根据名称查询列表")
    public Result listbyName(String flowName,
                             Integer level,
                             Long organizationId,
                             String isAllClient,
                             @RequestParam List<Long> clientIds) {
        List<ContingPlanFlow> contingPlanFlows = contingPlanFlowMapper.getcontingPlanFlowByName(organizationId, flowName, level, isAllClient, clientIds);
        return Result.success(contingPlanFlows);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(contingPlanFlowService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageSize * pageNum - pageSize;
        List<ContingPlanFlow> contingPlanFlows = contingPlanFlowMapper.findPage(pageNum, pageSize, organizationId, keyWord, level, isAllClient, clientIds);
        Long total = contingPlanFlowMapper.findPageCount(organizationId, keyWord, level, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", contingPlanFlows);
        map.put("total", total);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        contingPlanFlowService.export(response, organizationId, keyWord, level, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入应急预案流程")
    public void importData(MultipartFile file) {
        contingPlanFlowService.importData(file);
    }

}

